package br.com.syshealth.clinica.session;

import java.util.List;
import java.util.Set;


import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.hibernate.Session;
import org.hibernate.criterion.Example;

import br.com.syshealth.clinica.entity.Agenda;
import br.com.syshealth.clinica.entity.Medico;
import br.com.syshealth.clinica.entity.Medico;
//import br.com.syshealth.clinica.entity.Tipo;

@Stateless
public class MedicoDAO {
	
	
	private Medico medico;
	
	@PersistenceContext(unitName="clinica-syshealth-core")
	private EntityManager em;
//	private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("aluno");
	
	
	
	public void atualizar(Medico bean){
		Object ent = em.merge(bean);
		System.out.println("atualizando");
		em.persist(ent);
	}
	
	public void excluir( Medico bean) {
		Object ent = em.merge(bean);
		System.out.println("deletando");
		em.remove(ent);
	}
	
	public Medico getBean(int id) {
		medico= null;
		try{
			 medico = em.find(Medico.class, id);
			System.out.println("********************TIPOOPOOO"+ medico.getTipo());
		}catch(Exception e){
			
		}
		return medico;
	}
	
	public Medico getBeanCpf(String cpf) {
		medico= null;
		try{
			Query query = em.createQuery("select p from Medico p where cpf ='"+cpf+"'");
			medico = (Medico) query.getSingleResult();
			System.out.println("********************TIPOOPOOO"+ medico.getTipo());
		}catch(Exception e){
			
		}
		return medico;
	}
	
	public List<Medico> listar()
	{
		List<Medico> medicoList = null;
		try{
			Query query = em.createQuery("select p from Medico p");
			medicoList = query.getResultList();
		
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return medicoList;
	}
	
	
	public void salvar(Medico bean) {
		if(em == null)
		{
			System.out.println("nulll");
		}else{System.out.println("aberta");
			
//			bean.setTipo(em.find(Tipo.class, bean.getTipo().getIdTipo()));
//			
//			System.out.println(bean.getTipo().getDescTipo());
//			
//			bean.getTipo().getPessoas().add(bean);		
			em.persist(bean);
		}
	}
	
	
}
